<?php
// +----------------------------------------------------------------------
// | 用户模块
// +----------------------------------------------------------------------
// | Author: myh
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\service\UserService;
use app\admin\service\AuserService;
use app\common\lib\oss\Oss;

class User extends AdminController
{
    //分页列表
    public function page(){
        return UserService::page($this->request->get());
    }

    //详情
    public function info(){
        $id = $this->request->get("id");
        if (empty($id)) {
            return success();
        }
        return UserService::info($id);
    }

    //编辑
    public function edit(){
        $data = $this->request->post();
        //验证规则
        $validate = [
            'id' => 'require',
        ];
        //提示信息
        $message = [
            'id.require' => 'ID不能为空!',
        ];

        if(!empty($data['password'])){
            //如果密码不为空时，说明是修改密码，因此要进行密码校验
            $validate['password'] = 'require|regex:/^[a-zA-Z0-9_]{8,20}$/';
            $message['password.require'] = '密码不能为空!';
            $message['password.regex'] = '密码长度8~20位，包含字母数字下划线!';
        }else{
            //销毁变量
            unset($data['password']);
        }

        $this->validate($data, $validate, $message);
        return UserService::update($data);
    }

    //导出
    public function export(){
        AuserService::export($this->request->get());
    }

    //上传头像
    public function uploadAvatar(){
        $file = $this->request->file('file');
        $pathName = $file->getPathname();
        $originalName = $file->getOriginalExtension();

        $object = "avatar/".$file->hash('md5').".".$originalName;
        return success(Oss::uploadFile($object,$pathName));
    }
}
